// widget.cpp
#include "widget.h"
#include <QPaintEvent>
#include <QPainter>
#include <QCursor>
#include <QMetaEnum>
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent),
    m_frameTimer(new QTimer(this)),
    m_contextMenu(new QMenu(this))
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    installEventFilter(new DragFilter(this));  // 修复内存管理

    connect(m_frameTimer, &QTimer::timeout, [this]() {
        auto& frames = m_animationFrames[m_currentAction];
        if(!frames.isEmpty()) {
            static int index = 0;
            m_currentFrame = frames[index++ % frames.size()];
            update();
        }
    });

    setupMenu();
    initResources();
    playAnimation(Dance);
}

Widget::~Widget() = default;

void Widget::playAnimation(ActionType type)
{
    m_frameTimer->stop();
    m_currentAction = type;
    if(!m_animationFrames[type].isEmpty()) {
        m_frameTimer->start(80);
    }
}

void Widget::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    if(!m_currentFrame.isEmpty()) {
        QPixmap frame;
        frame.load(m_currentFrame.toLocalFile());
        painter.drawPixmap(0, 0, frame);
    }
}

void Widget::contextMenuEvent(QContextMenuEvent*)
{
    m_contextMenu->popup(QCursor::pos());
}

void Widget::handleMenuAction(QAction* action)
{
    QMetaEnum meta = QMetaEnum::fromType<ActionType>();
    int value = meta.keyToValue(action->text().toUtf8());
    if (value != -1) {
        playAnimation(static_cast<ActionType>(value));
    }
}

void Widget::initResources()
{
    auto addFrames = [this](ActionType type, const QString& pattern, int count) {
        QList<QUrl> frames;
        for (int i = 0; i < count; ++i) {
            QString path = QString(pattern).arg(i);
            frames.append(QUrl::fromLocalFile(path));
        }
        m_animationFrames.insert(type, frames);
    };

    addFrames(Dance, ":/dance/img/dance/dance_%1.png", 12);
    addFrames(Punish, ":/punish/img/punish/punish_%1.png", 14);
    addFrames(Run, ":/run/img/run/run_%1.png", 14);
}

void Widget::setupMenu()
{
    m_contextMenu->addAction("Dance");
    m_contextMenu->addAction("Punish");
    m_contextMenu->addAction("Run");

    QAction* hideAction = new QAction("Hide", this);
    connect(hideAction, &QAction::triggered, this, &Widget::hide);
    m_contextMenu->addAction(hideAction);

    connect(m_contextMenu, &QMenu::triggered, this, &Widget::handleMenuAction);
}

// 修复拖拽功能的核心实现
bool DragFilter::eventFilter(QObject* obj, QEvent* event)
{
    auto* widget = qobject_cast<QWidget*>(obj);
    if (!widget) return false;

    switch (event->type()) {
    case QEvent::MouseButtonPress: {
        auto* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->button() == Qt::LeftButton) {
            m_dragStartPos = mouseEvent->pos();
            m_dragging = true;
            return true;
        }
        break;
    }
    case QEvent::MouseMove: {
        if (m_dragging) {
            auto* mouseEvent = static_cast<QMouseEvent*>(event);
            QPoint newPos = mouseEvent->globalPosition().toPoint() - m_dragStartPos;
            widget->move(newPos);
            return true;
        }
        break;
    }
    case QEvent::MouseButtonRelease: {
        auto* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->button() == Qt::LeftButton) {
            m_dragging = false;
            return true;
        }
        break;
    }
    default:
        break;
    }
    return QObject::eventFilter(obj, event);
}
